
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IPerfplanperiodBO;
import com.wyzz.hr.dao.IPerfplanperiodDAO;
import com.wyzz.hr.pojo.Perfplanperiod;


public class PerfplanperiodBOImpl implements IPerfplanperiodBO {

	public PerfplanperiodBOImpl() {
	}

	public IPerfplanperiodDAO getPerfplanperiodDAO() {
		return perfplanperiodDAO;
	}

	public void setPerfplanperiodDAO(IPerfplanperiodDAO perfplanperiodDAO) {
		this.perfplanperiodDAO = perfplanperiodDAO;
	}

	public Perfplanperiod loadPerfplanperiod(String id) {
		return (Perfplanperiod) perfplanperiodDAO.loadObject(
				com.wyzz.hr.pojo.Perfplanperiod.class, id, null,
				new boolean[] { false });
	}

	public List searchUnexpiredPerfplanperiod() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfplanperiod.class);
		dc.add(Restrictions.ge(Perfplanperiod.PROP_PPP_END_DATE, new Date()));
		return perfplanperiodDAO.findByCriteria(dc);
	}

	public List searchAllPpp() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfplanperiod.class);
		dc.addOrder(Order.desc(Perfplanperiod.PROP_PPP_YEARMONTH));
		List pppList = perfplanperiodDAO.findByCriteria(dc);
		Map pppClassMap = ComonBeans.getValuesToMap("pppClass",
				new boolean[] { true });
		Perfplanperiod ppp;
		for (Iterator i$ = pppList.iterator(); i$.hasNext(); ppp
				.setPppClassName((String) pppClassMap.get(ppp.getPppClass())))
			ppp = (Perfplanperiod) i$.next();

		return pppList;
	}

	public Perfplanperiod save(Perfplanperiod ppp) {
		if (StringUtils.isEmpty(ppp.getPppId())) {
			if (getPppByMonthAndName(ppp.getPppYearMonth(), ppp.getPppName()) != null) {
				ppp.setInfo("EXISTED");
				return ppp;
			}
			perfplanperiodDAO.saveObject(ppp);
		} else {
			perfplanperiodDAO.updateObject(ppp);
		}
		Map pppClassMap = ComonBeans.getValuesToMap("pppClass",
				new boolean[] { true });
		ppp.setPppClassName((String) pppClassMap.get(ppp.getPppClass()));
		ppp.setInfo("SUCC");
		return ppp;
	}

	public Perfplanperiod getPppByMonthAndName(String yearMonth, String pppName) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfplanperiod.class);
		dc.add(Restrictions.eq(Perfplanperiod.PROP_PPP_YEARMONTH, yearMonth));
		dc.add(Restrictions.eq(Perfplanperiod.PROP_PPP_NAME, pppName.trim()));
		List pstList = perfplanperiodDAO.findByCriteria(dc);
		if (pstList != null && pstList.size() > 0)
			return (Perfplanperiod) pstList.get(0);
		else
			return null;
	}

	public void delete(String pppId) {
		perfplanperiodDAO.deleteObject(com.wyzz.hr.pojo.Perfplanperiod.class,
				pppId);
	}

	public int getRelatePpCount(String pppId) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Perfplan where ppPppId.pppId='").append(
				pppId).append("'").toString();
		List list = perfplanperiodDAO.exeHqlList(hql);
		if (list != null && list.size() > 0)
			return ((Long) list.get(0)).intValue();
		else
			return 0;
	}

	public List searchAllName() {
		String hql = "select pppName from Perfplanperiod ";
		return perfplanperiodDAO.exeHqlList(hql);
	}

	private IPerfplanperiodDAO perfplanperiodDAO;
}
